﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sinacor.Infra.UI.Controls.SmartClient;
using Sinacor.UI.Bovespa.Ordens.Presenter;
using PSE.Framework.UI.Resources;

namespace Sinacor.UI.Bovespa.Ordens.View.SmartClient
{
    /// <summary>
    /// Interaction logic for CadastroMotivo.xaml
    /// </summary>
    public partial class CadastroMotivos : SinacorWindow, ICadastroMotivo
    {
        #region PresenterCadastroMotivo

        public Presenter.CadastroMotivo _presenterCadastroMotivo { get; set; }

        #endregion

        #region Construtor

        public CadastroMotivos()
        {

            this.InitializeComponent();

            this._presenterCadastroMotivo = new Sinacor.UI.Bovespa.Ordens.Presenter.CadastroMotivo(this);

            this.DataContext = this._presenterCadastroMotivo;

            this.AfterInitialize += new RoutedEventHandler(CadastroMotivo_AfterInitialize);

            this.dataMotivos.NewItemType = typeof(Model.Motivo);

        }

        void CadastroMotivo_AfterInitialize(object sender, RoutedEventArgs e)
        {
            this.ShowUserWaitingProcess();

            this._presenterCadastroMotivo.Inicializar();

            this.HideUserWaitingProcess();
        }

        #endregion

        #region ICadastroMotivo Members

        public void AtualizarMotivos()
        {
            this.dataMotivos.UpdateTarget(SinacorDataGrid.ItemsSourceProperty);
        }

        public bool ConfirmarExclusaoMotivo()
        {
            bool corfirma = false;

            if (SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Ordens.Message.ConfirmaExclusaoMotivo").ToString(), SinacorMessageBoxButton.YesNo, SinacorMessageBoxImage.Question)
                == SinacorMessageBoxResult.Yes)
                corfirma = true;

            return corfirma;
        }

        public void InformarMotivoAtualizadoSucesso()
        {
            SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Ordens.Message.AtualizacaoMotivoSucesso"), SinacorMessageBoxImage.Information);
            this.AtualizarMotivos();
        }

        public void InformarMotivoIncluidoSucesso()
        {
            SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Ordens.Message.InclusaoMotivoSucesso"), SinacorMessageBoxImage.Information);
        }

        #endregion

        #region Eventos

        private void dataMotivos_Deleting(object sender, CancelItemsEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
                if (dataMotivos.SelectedItem != null)
                {
                    this.ClearServiceErrors();

                    if (this.ConfirmarExclusaoMotivo())
                    {
                        this.ShowUserWaitingProcess();

                        if (!this._presenterCadastroMotivo.ExcluirMotivo((this.dataMotivos.SelectedItem as Model.Motivo)))
                            e.Cancel = true;

                        HideUserWaitingProcess();
                        this.ClearChanges();
                    }
                    else
                    {
                        e.Cancel = true;
                        e.Handled = true;
                    }
                }
            }

        }

        private void dataMotivos_EndingInsert(object sender, CancelItemEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
                if (dataMotivos.GetInsertionRow() != null)
                {
                    this.ClearServiceErrors();

                    ShowUserWaitingProcess();

                    if (this._presenterCadastroMotivo.IncluirMotivo(this.dataMotivos.EditItem as Model.Motivo))
                    {
                        this.HideUserWaitingProcess();
                        e.Handled = true;
                        e.Cancel = true;
                    }
                    else
                    {
                        HideUserWaitingProcess();
                        this.ClearChanges();
                    }
                }
            }
        }

        private void dataMotivos_EndingUpdate(object sender, CancelItemEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
                if (this.dataMotivos.EditItem != null)
                {
                    this.ClearServiceErrors();

                    ShowUserWaitingProcess();

                    if (!this._presenterCadastroMotivo.AlterarMotivo((this.dataMotivos.EditItem as Model.Motivo)))
                    {
                        this.HideUserWaitingProcess();
                        e.Cancel = true;
                        e.Handled = true;
                    }
                    else
                    {
                        HideUserWaitingProcess();
                        this.ClearChanges();
                    }
                }
            }

        }

        private void dataMotivos_CancelingInsert(object sender, CancelItemEventArgs e)
        {
            this.ClearServiceErrors();
        }

        #endregion

    }

}

